﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Xml;
namespace ScintillaNET_Editor_Plugin {
    public partial class SettingsDialog : Form {
        private const string languageURL = "http://notepad-x.sourceforge.net/files/ScintillaNet/list";
        private string listFile = "";

        public SettingsDialog() {
            InitializeComponent();
        }
        private void ScanDirectory() {
            list.Items.Clear();
            foreach (string file in Directory.GetFiles(plugin.directory, "*.xml")) {
                try {
                    XmlDocument xmlFile = new XmlDocument();
                    xmlFile.Load(file);
                    string Name = xmlFile.SelectSingleNode("ScintillaNET/Language").Attributes["Name"].InnerText;

                    //if (Name.ToLower() == "default") {
                    //    languageList.Add(new Language(Name, null));
                    //    continue;
                    //}
                    if (xmlFile.SelectSingleNode("ScintillaNET/Language").Attributes["Extensions"] != null) {
                        ListViewItem li = new ListViewItem(Name);
                        li.SubItems.Add(file);
                        list.Items.Add(li);
                    }
                } catch (Exception) {
                }
            }
        }
        private void readList() {
            list.Items.Clear();
            if (!File.Exists(listFile)) return;
            XmlDocument xmlFile = new XmlDocument();
            xmlFile.Load(listFile);

            XmlNodeList nodes = xmlFile.GetElementsByTagName("Language");

            foreach (XmlNode node in nodes) {
                ListViewItem li = new ListViewItem(node.Attributes["Name"].InnerText);
                li.SubItems.Add(node.Attributes["File"].InnerText);
                li.SubItems.Add(node.InnerText);
                list.Items.Add(li);
            }
        }
        private void radio_CheckedChanged(object sender, EventArgs e) {
            updateList.Enabled= !radioInstalled.Checked;

            if (radioInstalled.Checked) {
                ScanDirectory();
                //Update list
            }else{
                readList();
            }
            list_SelectedIndexChanged(null, null);
        }

        private void SettingsDialog_Load(object sender, EventArgs e) {
            if (Notepad_X.Static.MainInstance.IconsCollection.Images.ContainsKey("options"))
                this.Icon = System.Drawing.Icon.FromHandle(new System.Drawing.Bitmap(Notepad_X.Static.MainInstance.IconsCollection.Images["options"]).GetHicon());
            
            listFile = Path.Combine(plugin.directory, "list");
            radioInstalled.Checked = true;
            list.Columns[0].Text = Notepad_X.Main.app_language[Notepad_X.Classes.Strings.Language];
            highlighters.Text = Notepad_X.Main.app_language[Notepad_X.Classes.Strings.Highlighters];
            highlighters.Checked = plugin.Highlighters;
            Notepad_X.Components.Static.UpdateFormControls(this, true);
        }

        private void list_SelectedIndexChanged(object sender, EventArgs e) {
            if (list.SelectedItems.Count > 0) {
                remove.Enabled = edit.Enabled = radioInstalled.Checked;
                download.Enabled = radioAvailable.Checked;
            } else {
                remove.Enabled = edit.Enabled = download.Enabled = false;
            }
        }

        private void remove_Click(object sender, EventArgs e) {
            for (int i = list.SelectedItems.Count-1; i >= 0; i--) {
                System.IO.File.Delete(list.SelectedItems[i].SubItems[1].Text);
                list.Items.Remove(list.SelectedItems[i]);
                   
            }
        }

        private void button3_Click(object sender, EventArgs e) {
            System.Diagnostics.Process.Start(plugin.directory);
        }

        private void updateList_Click(object sender, EventArgs e) {
            Notepad_X.Download dl = new Notepad_X.Download(languageURL, listFile, Notepad_X.DownloadTypes.Autoclose);
            dl.DownloadCompletedEvent += new Notepad_X.Download.DownloadCompletedHandler(dl_DownloadCompletedEvent);
            dl.ShowDialog(this);
        }

        void dl_DownloadCompletedEvent(object sender, Notepad_X.DownloadCompleted e) {

            if (e.FilePath == listFile) {
                status.Text = "List updated!";
                status.Visible = true;
                statusTimer.Start();
                readList(); 
            } else {
                status.Text = "File downloaded!";
                status.Visible = true;
                statusTimer.Start();
                //return;
                //XmlDocument xmlFile = new XmlDocument();
                //xmlFile.Load(e.FilePath);

                //XmlNodeList nodes = xmlFile.GetElementsByTagName("Language");

                //foreach (XmlNode node in nodes) {
                //    ListViewItem li = new ListViewItem(node.Attributes["Name"].InnerText);
                //    li.SubItems.Add(node.InnerText);
                //    list.Items.Add(li);

                //}
            }


        }

        private void edit_Click(object sender, EventArgs e) {
            for (int i = list.SelectedItems.Count -1 ; i >= 0; i--) {
               Notepad_X.Static.MainInstance.system_open(list.SelectedItems[i].SubItems[1].Text);
            }
        }

        private void download_Click(object sender, EventArgs e) {
            for (int i = 0; i < list.SelectedItems.Count; i++) {
                Notepad_X.Download dl = new Notepad_X.Download(list.SelectedItems[i].SubItems[2].Text, Path.Combine(plugin.directory, list.SelectedItems[0].SubItems[1].Text), Notepad_X.DownloadTypes.Autoclose);
                dl.DownloadCompletedEvent += new Notepad_X.Download.DownloadCompletedHandler(dl_DownloadCompletedEvent);
                dl.Show();
            }
        }

        private void statusTimer_Tick(object sender, EventArgs e) {
            status.Visible = false;
            statusTimer.Stop();
        }

        private void SettingsDialog_FormClosing(object sender, FormClosingEventArgs e) {
            plugin.Highlighters = highlighters.Checked;
        }


     
    }
}
